package per.turtle.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import per.turtle.common.core.exception.CustomException;
import per.turtle.system.entity.SysPost;
import per.turtle.system.mapper.SysPostMapper;
import per.turtle.system.mapper.SysUserPostMapper;
import per.turtle.system.service.ISysPostService;

import java.util.List;

/**
 * 岗位信息 服务层实现
 *
 * @author turtle
 */
@RequiredArgsConstructor
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    private final SysUserPostMapper userPostMapper;

    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        // 是否存在与该数据同 PostCode 的其他数据
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StrUtil.isNotBlank(post.getPostCode()), SysPost::getPostCode, post.getPostCode())
               .ne(post.getPostId() != null, SysPost::getPostId, post.getPostId());
        List<SysPost> posts = baseMapper.selectList(wrapper);
        return posts.isEmpty();
    }

    @Override
    public boolean checkPostNameUnique(SysPost post) {
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StrUtil.isNotBlank(post.getPostName()), SysPost::getPostName, post.getPostName())
               .ne(post.getPostId() != null, SysPost::getPostId, post.getPostId());
        List<SysPost> posts = baseMapper.selectList(wrapper);
        return posts.isEmpty();
    }

    @Override
    public int countUserPostById(Long postId) {
        return userPostMapper.countUserPostById(postId);
    }

    @Override
    public int deletePostById(Long postId) {
        return baseMapper.deleteById(postId);
    }

    @Override
    public int deletePostByIds(List<Long> postIds) {
        for (Long postId : postIds) {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                throw new CustomException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return baseMapper.deleteBatchIds(postIds);
    }

    @Override
    public int insertPost(SysPost post) {
        return baseMapper.insert(post);
    }

    @Override
    public List<SysPost> selectPostAll() {
        return baseMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public SysPost selectPostById(Long postId) {
        return baseMapper.selectById(postId);
    }

    @Override
    public List<SysPost> selectPostList(SysPost post) {
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>(post);
        wrapper.getEntity().setPostName(null);
        wrapper.getEntity().setPostCode(null);
        wrapper.lambda().like(StrUtil.isNotBlank(post.getPostName()), SysPost::getPostName, post.getPostName())
               .like(StrUtil.isNotBlank(post.getPostCode()), SysPost::getPostCode, post.getPostCode());
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<String> selectPostListByUserId(String userId) {
        return baseMapper.selectPostListByUserId(userId);
    }

    @Override
    public int updatePost(SysPost post) {
        return baseMapper.updateById(post);
    }

}
